package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Emp;
import com.by.common.bean.PageBean;
import com.by.mapper.UserMapper;
import com.by.query.EmpQuery;
import com.by.service.EmpService;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "员工管理")
public class EmpController extends BaseController {

    @Autowired
    private EmpService empService;
    @Autowired
    private UserMapper userMapper;

    //get：查  post：增  delete：删  put：改

    @ApiOperation("查询客户")
    @GetMapping("/emp")
    public ResultBean get(String id) {
        Emp emp = empService.get(id);
        return success("客户信息", emp);
    }

    @ApiOperation("新增员工")
    @PostMapping("/emp")
    public ResultBean save(@Validated(ISave.class) @RequestBody Emp emp, BindingResult result) {
        V.validate(result);
        int rs=empService.save(emp);
        return saveResult(rs);
    }

    @ApiImplicitParam(name = "id", value = "客户id", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除员工")
    @DeleteMapping("/emp")
    public ResultBean delete(@RequestParam(required = true) int id) {
        int rs = empService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("修改员工信息")
    @PutMapping("/emp")
    public ResultBean update(@RequestBody Emp emp){
        int rs=empService.update(emp);
        return updateResult(rs);
    }

    @ApiOperation("查询员工列表")
    @PostMapping("/emp/list")
    public ResultBean<PageBean<List<Emp>>> query(@RequestBody EmpQuery query) {
        PageBean bean = empService.queryByMap(BeanUtil.beanToMap(query));
        return success("客户列表", bean);
    }
}
